package _99练习;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @author ZhiCheng Qian
 * @since 2021/11/30 12:13
 */
public class ChangeBackground extends JFrame {

	// 5. 编写一个GUI界面程序
	// 创建一个子线程
	// 在子线程中定时修改界面背景颜色为随机色

	public ChangeBackground() {

		this.setLayout(null);

		JButton btn1 = new JButton("测试1");
		btn1.setBounds(0, 0, 100, 50);

		JPanel p = new JPanel();
		p.setBounds(0, 50, 100, 100);

		this.add(btn1);
		this.add(p);

		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Thread t = new Thread(new Runnable() {
					@Override
					public void run() {
						Random rd = new Random();
						int r = rd.nextInt(255);
						int g = rd.nextInt(255);
						int b = rd.nextInt(255);
						Color color = new Color(r, g, b);
						p.setBackground(color);
					}
				});
				t.start();
			}
		});

	}
}
